//
//  BlogCell.m
//  VICKY ZONE
//
//  Created by sunx on 15/3/23.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "BlogCell.h"
#import "ImageLoader.h"
@interface BlogCell()
{
    int refreshtimes;
}
@property(nonatomic,strong) NSTimer *timer;
@property(nonatomic,strong) NSMutableArray *images;
@property(nonatomic,strong) NSArray *imageURLs;
@end
@implementation BlogCell
-(instancetype)initWithTitle:(NSString *)title andBody:(NSString *)body andDate:(NSString *)date andImages:(NSArray *)imageURLs {
    self = [super init];
    [self setBackgroundColor:[UIColor clearColor]];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIColor *defaultColor = [UIColor colorWithRed:92.0/255 green:92.0/255 blue:92.0/255 alpha:1];
    UIColor *textColor = [UIColor colorWithRed:198.0/255 green:198.0/255 blue:198.0/255 alpha:1.0];

    refreshtimes = 0;
    
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    float realWidth = r.size.width;
    
    float titleHeight = 40;
    self.title = [[UILabel alloc] init];
    self.title.text = title;
    self.title.frame = CGRectMake(15, 0, realWidth, titleHeight);
    self.title.backgroundColor = [UIColor clearColor];
    self.title.font = [UIFont fontWithName:@"Helvetica" size:25];
    self.title.textColor = textColor;
    [self addSubview:self.title];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(refreshSelf) userInfo:nil repeats:YES];
    
    float imagesHeight = 0;
    
    self.imageURLs = imageURLs;
    if(imageURLs != nil) {
        self.images = [NSMutableArray new];
        for(int i = 0; i < [imageURLs count]; i ++) {
            UIImage *image = [ImageLoader loardImageToDocuments:imageURLs[i]];
            
            if(image == nil) {
                image = [UIImage imageNamed:@"article"];
                [self.timer fire];
            }
            
            [self.images addObject:image];
        }
        
        self.imageViews = [NSMutableArray new];
        for(int i = 0; i < [imageURLs count]; i ++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            UIImage *image = (UIImage *)self.images[i];
            imageView.image = image;
            
            float height = image.size.height / image.size.width * realWidth;
            imageView.frame = CGRectMake(0, imagesHeight+titleHeight, realWidth, height);
            imagesHeight += height;
            
            [self.imageViews addObject: imageView];
            [self addSubview:imageView];
        }
        
    }
    
    //内容body
    self.body = [[UILabel alloc] initWithFrame:CGRectMake(realWidth*0.1, 10, realWidth *0.8, 100)];
    
    self.body.font = [UIFont fontWithName:@"Helvetica" size:15];
    self.body.text = body;
    self.body.backgroundColor = [UIColor clearColor];
    self.body.textColor = textColor;
    //self.body.editable = NO;
    self.body.alpha = 0.7;
    self.body.numberOfLines = 0;
    [self.body sizeToFit];
    //计算text view高度
    float bodyHeight = self.body.frame.size.height + self.body.frame.origin.y + 20;
    /*
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        
        CGRect textFrame=[[self.body layoutManager]usedRectForTextContainer:[self.body textContainer]];
        bodyHeight = textFrame.size.height;
        
    }else {
        bodyHeight = self.body.contentSize.height;
    }
    bodyHeight += 20;
    //设置高度
    self.body.frame = CGRectMake(realWidth*0.05, 0, realWidth *0.9, bodyHeight);
    */
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, imagesHeight +titleHeight, realWidth, bodyHeight)];
    [view setBackgroundColor:defaultColor];
    [view addSubview:self.body];
    [self addSubview:view];
    
   
    
    float dateHeight = 20;
    self.date = [[UILabel alloc] init];
    self.date.text = date;
    self.date.frame = CGRectMake(realWidth - 90, titleHeight+imagesHeight - 25, 80, dateHeight);
    self.date.font = [UIFont fontWithName:@"Helvetica" size:15];
    self.date.textColor = [UIColor whiteColor];
    [self addSubview:self.date];

    self.height = titleHeight + imagesHeight + bodyHeight + 50;
    return self;
}
- (void)refreshSelf {
    NSLog(@".");
    refreshtimes ++;
    BOOL isEnd = YES;
    for(int i = 0; i < [self.imageURLs count]; i++) {
        UIImageView *imageView = (UIImageView *)self.imageViews[i];
        UIImage *image = [ImageLoader loardImageToDocuments:self.imageURLs[i]];
        if(image == nil) {
            refreshtimes ++;
            isEnd = NO;
        }
        imageView.image = image;
        if(refreshtimes > 15) {
            isEnd  = YES;
        }
    }
    if(isEnd) {
        [self.timer invalidate];
    }
    
}
@end
